<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"/>
</template>

<script>
  import echarts from 'echarts'
  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      id: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '200px'
      },
      height: {
        type: String,
        default: '200px'
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.initChart()
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(document.getElementById(this.id))
        // https://echarts.baidu.com/examples/editor.html?c=bar-label-rotation
        const itstyle= {
          normal: {
            label: {
              show: true,
              textStyle: {
                fontSize: 12
              },
              formatter: function(params){ //避免文字太长做省略处理
                console.log(params);
                var name=params.name; //名字
                var value=params.value[2]; //数量
                return name+"\n"+"("+value+"%)";
              }
            },
            labelLine: {
              show: true
            },
          },
          emphasis: {
            shadowBlur: 5,
            shadowOffsetX: 10,
            shadowColor: 'rgba(0, 0, 0, 0.3)'
          }
        };
        let option = {
          legend: {
            top: '55%',
            left: '36%',
          },
          tooltip: {},
          title: {
            // top: 20,
            top: '5%',
            left: '41%',
            text: ['月度完成率 / 年度单耗完成率'],
            textStyle: {
              // fontWeight: 'normal',
              fontSize: 20,
              color: '#2d7d75'
            },
          },
          dataset: {
            source: [
              ['area', '月度完成率', '年度单耗完成率'],
              ['云南区域', 39.90, 76],
              ['陕甘区域', 39.84, 101],
              ['皖北区域', 27.00, 97],
              ['川渝区域', 23.26, 90],
              ['广东区域', 17.9,  95]
            ]
          },
          series: [{
            type: 'pie',
            radius: 70,
            center: ['30%', '30%']
            // No encode specified, by default, it is '2012'.
          }, {
            type: 'pie',
            radius: 70,
            center: ['70%', '30%'],
            encode: {
              itemName: 'area',
              value: '年度单耗完成率'
            },
            itemStyle: itstyle
          },
          ]
        };

        this.chart.setOption(option)
        console.log(this.chart)
//===================================================
      }
    }
  }
</script>
